package net.lainiao.morethread.Chapter1.demo7;

import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2017/2/28.
 */
public class Main {

    public static void main(String[] args) {
        ThreadGroup threadGroup = new ThreadGroup("Searcher");
        Result result = new Result();
        SearchTask searchTask = new SearchTask(result);

        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(threadGroup, searchTask);
            thread.setName("MyThread-"+i);
            thread.start();
        }

        try {
            TimeUnit.SECONDS.sleep(1);
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("Number of Thread Actived :" + threadGroup.activeCount());
        System.out.println("Infomation about the Thread Group");
        threadGroup.list();

        Thread[] threads=new Thread[threadGroup.activeCount()];
        threadGroup.enumerate(threads);
        for (int i = 0; i <threadGroup.activeCount() ; i++) {
            System.out.println("Thread "+threads[i].getName()+" Statue "+threads[i].getState());
        }

        waiteFinish(threadGroup);
        threadGroup.interrupt();
    }

    private static void waiteFinish(ThreadGroup threadGroup) {
        while (threadGroup.activeCount()>9){
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
